<?php
/**
 * @Author: your name
 * @Date:   2017-05-17 09:47:53
 * @Last Modified by:   your name
 * @Last Modified time: 2017-05-27 11:46:32
 */
namespace app\index\controller;
use think\Controller;
class Register extends Controller
{
    public function _initialize() {
        parent::_initialize();
        $this->uid = session('wx_uid');
    }
    /**
     * 注册
     */
    public function index()
    {
        if(request()->isPost()){
            $mobile = input('phone');
            $code  = (int)input('code');
            if(empty($code)){
                return json(['status'=>0,'message'=>'验证码不能为空']);
            }
            if($code !== session('regCode')){
                return json(['code'=>$code,'regcode'=>session('regCode'),'status'=>0,'message'=>'验证码错误']);
            }else{
                $user['mobile'] = $mobile;
                $user['create_time'] = time();
                //判断手机是否注册过
                $val= db('user')->where('mobile',$mobile)->find();
                if(empty($val)){
                    $id = db('user')->where('id',session('wx_uid'))->update(array('mobile'=>$mobile));
                    if($id){
                        return json(['status'=>1,'message'=>'注册成功']);
                    }else{
                        return json(['status'=>1,'message'=>'注册失败']);
                    }
                }else{
                    return json(['status'=>0,'message'=>'该手机号已注册，请勿重复注册']);
                }
            }
        }else{
            return $this->fetch();
        }


    }
    /**
     * 发送手机验证码
     */
    public function code()
    {
        session('regCode',null);//清除缓存
        $code=rand(1000,9999);
        $send['mobile'] = input('phone');
        if(empty($send['mobile'])) {
            return json(['status'=>0,'message'=>'手机号码不能为空']);
        }
        $send['content'] ="【拉米科技】您好,您在医药小伙伴上的注册验证码为".$code.",请及时验证，有效期为30分钟";
        sendMobile($send);
        session('regCode',$code);
        return json(['status'=>1,'code'=>$code,'message'=>'发送成功,请注意查收']);



    }






}